<?php
include_once __DIR__ . '/../autoload.php';
include_once dirname(dirname(__FILE__)) . '/filtro_usuario.php';
include_once dirname(dirname(__FILE__)) . '/funciones/basicas.php';
// include_once dirname(dirname(__FILE__)) . '/datos/VendedorDao.php';
use datos\VendedorDao;
$conexion = conectarPDO();
$vendedorDao = new VendedorDao($conexion);
//echo $_POST['proveedor'];
if( isset($_POST['buscar']) ){
    //Recibe codigo de vendedor, mes y a�o que se desea listar
    $vendedor = $_POST['vendedor'];
    $mes = $_POST['mes'];
    $ano = $_POST['ano'];   
    $result = $vendedorDao->getVentasPorVendedor($vendedor, $mes, $ano);       
    if(count($result) > 0){        
        echo "<table cellpadding=5 cellspacing=0 bgcolor=#000000>
                <tr>
		<td bgcolor=#000066><font color=#ffffff face=Arial, Helvetica, sans-serif>N� Factura</font></td>
                <td bgcolor=#000066><font color=#ffffff face=Arial, Helvetica, sans-serif>Cliente</font></td>
                <td bgcolor=#000066><font color=#ffffff face=Arial, Helvetica, sans-serif>Fecha de venta</font></td>
                <td bgcolor=#000066><font color=#ffffff face=Arial, Helvetica, sans-serif>Descripci�n</font></td>
                <td bgcolor=#000066><font color=#ffffff face=Arial, Helvetica, sans-serif>Cantidad</font></td>
                <td bgcolor=#000066><font color=#ffffff face=Arial, Helvetica, sans-serif>Precio de lista</font></td>
                <td bgcolor=#000066><font color=#ffffff face=Arial, Helvetica, sans-serif>%Descuento</font></td>";
        if($_SESSION['perfil'] == 'Admin'):
            echo "<td bgcolor=#000066><font color=#ffffff face=Arial, Helvetica, sans-serif>Costo unitario</font></td>";
        endif;
        echo "<td bgcolor=#000066><font color=#ffffff face=Arial, Helvetica, sans-serif>Precio de venta unitario</font></td>
            <td bgcolor=#000066><font color=#ffffff face=Arial, Helvetica, sans-serif>Neto</font></td>
            <td bgcolor=#000066><font color=#ffffff face=Arial, Helvetica, sans-serif>Iva</font></td>
            <td bgcolor=#000066><font color=#ffffff face=Arial, Helvetica, sans-serif>Total</font></td>
            <td bgcolor=#000066><font color=#ffffff face=Arial, Helvetica, sans-serif>Forma de Pago</font></td>";
        if($_SESSION['perfil'] == 'Admin'):
            echo "<td bgcolor=#000066><font color=#ffffff face=Arial, Helvetica, sans-serif>Ver Factura</font></td>";
        endif; 
        echo "<td bgcolor=#000066><font color=#ffffff face=Arial, Helvetica, sans-serif>Ver Producto</font></td>
            </tr>";
			
        $totalNetos = 0;
	$costototal = 0;
        $i = true;
        foreach ($result as $fila){
            (($i = !$i))? $color = "#CCCC99" : $color = "#ffffff";
            //$fila = mysql_fetch_array($consulta);			   
            //Suma los netos de cada producto de cada factura
            $costototal = $costototal + ($fila['costo_unitario'] * $fila['cantidad']);
            $totalNetos += $fila['neto'];
            $nfactura = $fila["cod_factura"];
            echo "<tr>";
            echo "<td bgcolor=".$color." class=Estilo2>";
            echo "<font color=#000000 face=Arial, Helvetica, sans-serif size=1.5>".$fila["cod_factura"]."</font>";
            echo "</td>";
            echo "<td bgcolor=".$color." class=Estilo2>";
            echo "<font color=#000000 face=Arial, Helvetica, sans-serif size=1.5>".$fila["cliente"]."</font>";
            echo "</td>";
            echo "<td bgcolor=".$color." class=Estilo2>";
            echo "<font color=#000000 face=Arial, Helvetica, sans-serif size=1.5>".FormatoFecha($fila["fecha_ingreso"])."</font>";
            echo "</td>";
            echo "<td bgcolor=".$color." class=Estilo2>";
            echo "<font color=#000000 face=Arial, Helvetica, sans-serif size=1.5>".$fila["nombre"]."</font>";
            echo "</td>";
            echo "<td bgcolor=".$color." class=Estilo2>";
            echo "<font color=#000000 face=Arial, Helvetica, sans-serif size=1.5>".$fila["cantidad"]."</font>";
            echo "</td>";
            echo "<td bgcolor=".$color." class=Estilo2>";
            echo "<font color=#000000 face=Arial, Helvetica, sans-serif size=1.5>$".number_format($fila['precio'],0,",",".")."</font>";
            echo "</td>";
            echo "<td bgcolor=".$color." class=Estilo2>";
            echo "<font color=#000000 face=Arial, Helvetica, sans-serif size=1.5>".$fila["descuento"]."</font>";
            echo "</td>";
            if($_SESSION['perfil'] == 'Admin'):
                echo "<td bgcolor=".$color." class=Estilo2>";
                echo "<font color=#000000 face=Arial, Helvetica, sans-serif size=1.5>$".number_format($fila['costo_unitario'],0,",",".")."</font>";
                echo "</td>";
            endif;
            echo "<td bgcolor=".$color." class=Estilo2>";
            echo "<font color=#000000 face=Arial, Helvetica, sans-serif size=1.5>$".number_format($fila['precio_venta'],0,",",".")."</font>";
            echo "</td>";
            echo "<td bgcolor=".$color." class=Estilo2>";
            echo "<font color=#000000 face=Arial, Helvetica, sans-serif size=1.5>$".number_format($fila['neto'],0,",",".")."</font>";
            echo "</td>";
            echo "<td bgcolor=".$color." class=Estilo2>";
            echo "<font color=#000000 face=Arial, Helvetica, sans-serif size=1.5>$".number_format($fila['iva'],0,",",".")."</font>";
            echo "</td>";
            echo "<td bgcolor=".$color." class=Estilo2>";
            echo "<font color=#000000 face=Arial, Helvetica, sans-serif size=1.5>$".number_format($fila['total'],0,",",".")."</font>";
            echo "</td>";
            echo "<td bgcolor=".$color." class=Estilo2>";
            echo "<font color=#000000 face=Arial, Helvetica, sans-serif size=1.5>".$fila['forma_pago']."</font>";
            echo "</td>";
            if($_SESSION['perfil'] == 'Admin'):
                echo "<td bgcolor=".$color." class=Estilo2>";
                ?>
                <a href="javascript:return false;" onclick="javascript:show('<?php echo $_SESSION['BASE'] . '/factura/ver_factura.php?nfactura=' . $fila['cod_factura'] . '&cliente=' . $fila['cod_cli'] ?>', 500, 900, 1);">
                <?php
                echo "<font color=#000000 face=Arial, Helvetica, sans-serif size=1.5>Detalle</font>";
                echo "</a>";
                echo "</td>";
            endif;
            echo "</td>";
            echo "<td bgcolor=".$color." class=Estilo2>";
            ?>
            <a href="javascript:return false;" onClick="javascript:show('<?php echo $_SESSION['BASE'] . '/mantenedor_productos/detalle_producto.php?codigo=' . $fila['cod_prod'] . '&viene=0' ?>', 500, 900,1);">
            <?php
            echo "<font color=#000000 face=Arial, Helvetica, sans-serif size=1.5>Detalle</font>";
            echo "</a>";
            echo "</td>";
            echo "</tr>";
        }
        $sql = urlencode($sql);
	?>
        </table>
	<br />Total de Netos: <?php echo "$" . number_format($totalNetos,0,",",".") ?>
        <?php
        if($_SESSION['perfil'] == 'Admin'): ?>
            <br />Total de Costos: <?php echo "$" . number_format($costototal,0,",",".") ?>
            <br />Margen Porcentual: <?php echo round((((($costototal/$totalNetos)-1)*100)*-1)) . "%" ?> 
        <?php endif; ?>
        <br />
        <form action="<?php echo $_SESSION['BASE'] . '/funciones/excel.php' ?>" method="post">
	<input type="hidden" name="sql" value="<?php echo $sql ?>">
        <input type="image" src="<?php echo $_SESSION['BASE'] . '/img/excel.png' ?>" name="excel" value="excel">
        </form>	
	<?php

    } else {
        echo "No hubo resultados para su busqueda.";
    }
} else{
   echo "Error";
}
?>